package com.lieve.tx.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author sunyoboy
 * 2020/9/20
 */
public class JDKProxyMain {

    public static void main(String[] args) {
        JDKProxyTestInterfaceImpl target = new JDKProxyTestInterfaceImpl();
        JDKProxyTestInterface testInterface = (JDKProxyTestInterface) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new JDKProxyTestInvocationHandler(target));
        testInterface.testProxy();
    }

    interface JDKProxyTestInterface {
        void testProxy();
    }

    static class JDKProxyTestInterfaceImpl implements JDKProxyTestInterface {
        @Override
        public void testProxy() {
            System.out.println("testProxy");
        }
    }

    static class JDKProxyTestInvocationHandler implements InvocationHandler {

        private Object target;

        JDKProxyTestInvocationHandler(Object target) {
            this.target = target;
        }

        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("before");
            Object result = method.invoke(this.target,args);
            System.out.println("after");
            return result;
        }
    }

}
